package com.ChenYaRou.www.dao.impl;

import com.ChenYaRou.www.dao.TicketDao;
import com.ChenYaRou.www.po.Ticket;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName TicketDaoImpl
 * @Description TODO
 * @Author 86183
 * @Date 2022/4/12 13:47
 * @Version 1.0
 **/
public class TicketDaoImpl implements TicketDao {
    /**
     * 查找抵用券集合
     * @param ticket 仅拥有条件赋值的抵用券
     * @return 抵用券集合
     * @throws  IllegalAccessException 非法访问
     * @throws SQLException sql error
     * @throws NoSuchFieldException 没有对应的实体类成员
     * @throws InstantiationException
     */
    @Override
    public List<Ticket> select(Ticket ticket) throws IllegalAccessException,
            SQLException, NoSuchFieldException, InstantiationException {
        return JdbcUtil.getInstance().selectMore(GenerateSql.getInstance().select(ticket), Ticket.class);
    }

    /**
     * 更新抵用券
     * @param newTicket ticket需要更新的值
     * @param  oldTicket old ticket
     * @return 成功与否
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @Override
    public int update(Ticket newTicket,Ticket oldTicket) throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().update(newTicket,oldTicket));
    }

    /**
     * 插入抵用券
     * @param ticket 新抵用券
     * @return 更新条数
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @Override
    public int insert(Ticket ticket) throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().insert(ticket));
    }

    /**
     * 所有ticket查找
     * @return
     * @throws SQLException
     * @throws NoSuchFieldException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    @Override
    public List<Ticket> selectAll() throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        return JdbcUtil.getInstance().selectMore(GenerateSql.getInstance().selectAll(new Ticket()), Ticket.class);
    }
}
